﻿namespace BM.Core
{
    using System;
    using System.IO;

    public class Log
    {
        public const string LOG_FILE = "log.txt";
        private static StreamWriter sw;

        /// <summary>
        /// Init new stream writer to write log to logfile.
        /// </summary>
        public static void Create()
        {
            sw = new StreamWriter(File.OpenWrite(LOG_FILE));
        }

        /// <summary>
        /// Write input to log file
        /// </summary>
        /// <param name="text"></param>
        public static void Write(string text)
        {
            if (sw == null) return;
            sw.WriteLine(DateTime.Now.ToString() + "--" + text);
            sw.Flush();
        }

        /// <summary>
        /// Write formatted inut to log file
        /// </summary>
        /// <param name="format">format pattern</param>
        /// <param name="param">params</param>
        public static void WriteFormat(string format, params object[] param)
        {
            if (sw == null) return;
            var content = string.Format(format, param);
            sw.WriteLine(DateTime.Now.ToString() + "--" + content);
            sw.Flush();
        }

        /// <summary>
        /// Close file log stream
        /// </summary>
        public static void Close()
        {
            try {
                if (sw != null) sw.Close();
            }
            catch (Exception) {
                throw;
            }
        }
    }
}
